package com.tyt.lelan.adapter;


import com.tyt.lelan.R;
import com.tyt.lelan.provider.FeedsData;
import com.tyt.lelan.utils.ImageUtil;
import com.tyt.lelan.utils.Property;

import android.content.Context;
import android.database.Cursor;

import android.util.Config;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

public class FeedAdapter extends ResourceCursorAdapter {

	/**
	 * @param context
	 * @param layout
	 * @param c
	 * @param autoRequery
	 */	
	private int[] index;
	private Cursor mCursor;
	private int mType;
	private Context mContext;
	private int page;
	
	private final int ITEM_MORE=0x1;
	private final int ITEM_NORMAL=0x0;
	
	public FeedAdapter(Context context, int layout, Cursor c,int p) {
		super(context, layout, c, true);
		// TODO Auto-generated constructor stub
		this.mCursor=c;
		this.mContext=context;
		this.page=p;

		int length = FeedsData.MyFeedsColumns.COLUMNS.length;
		index= new int[length];
		for(int i=0;i<length;i++){
			index[i] =c.getColumnIndex(FeedsData.MyFeedsColumns.COLUMNS[i]);
		}
	}
	
	 protected void onContentChanged() {
		 Log.i("onContentChanged", "########################");
		 Log.i("onContentChanged", "onContentChanged");
		 Log.i("onContentChanged", "########################");
		 super.onContentChanged();
	 
	    }
	
	
	/* (non-Javadoc)
	 * @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
	 */
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		if(cursor.isClosed()||cursor.getCount()==0) return;
		  final ViewHolder cache = (ViewHolder) view.getTag();
		  if(ITEM_MORE==mType){
			  cache.image_icon.setImageBitmap(
						ImageUtil.ReflectionImage(context.getResources().getDrawable(R.drawable.more)));
			  cache.tx_feedname.setText("更多内容");
			  mType=ITEM_NORMAL;
		  }
		  
		  else{
			  cache.tx_feedname.setText(cursor.getString(index[2]));
			  cache.image_icon.setImageBitmap(
						ImageUtil.ReflectionImage(context.getResources().getDrawable(R.drawable.letv)));
		  }
		  
		  /*
		  else{			  
			  cache.tx_feedname.setText(cursor.getString(index[2]));
	
			  String iconname=cursor.getString(index[4]);
			  Cursor cursor_myIcon=mContext.getContentResolver().query(FeedsData.IconColumns.CONTENT_URI_ICONS,
					  new String[]{FeedsData.IconColumns.ICON}, FeedsData.NAME+"=? ",new String[]{iconname} , null);
			  if(0!=cursor_myIcon.getCount()){ 
				  				  
				  Log.i("=Blob=", cursor.getBlob(0).toString());
				  ByteArrayInputStream stream = new ByteArrayInputStream( 
							cursor.getBlob(0)); 
				  BitmapDrawable bd = new BitmapDrawable(stream);
				  cache.image_icon.setImageBitmap(bd.getBitmap());
				  
				  
			  }
			  else{				  
				  cache.image_icon.setImageBitmap(
							ImageUtil.ReflectionImage(context.getResources().getDrawable(R.drawable.letv)));
			  }
			 
		  }
		  */
	}

	/* (non-Javadoc)
	 * @see android.widget.ResourceCursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
	 */
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = super.newView(context, cursor, parent);
		ViewHolder holder = new ViewHolder();
		holder.image_icon=(ImageView) view.findViewById(R.id.iv_Icon);
		holder.tx_feedname=(TextView) view.findViewById(R.id.tv_Name);
        view.setTag(holder);
		return view;
	
	}
	
	
	private class ViewHolder{
		
		private ImageView image_icon;
		private TextView  tx_feedname;
	}
	
	 public int getCount() {
	      
		//判断当前页面，控制返回的数据量，实现分页
			int i = getPage() * Property.PAGE_SIZE_MYFEED;
			int iEnd = i+Property.PAGE_SIZE_MYFEED;
			int size=mCursor.getCount();
			if(size>=i&&size<iEnd)
				return (size-i+1);
			else
				return Property.PAGE_SIZE_MYFEED;
	        
	    }
	 
		/* (non-Javadoc)
		 * @see android.widget.CursorAdapter#getItem(int)
		 */
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			Cursor mCursor = (Cursor) super.getItem(position);
			mCursor.moveToPosition(getPage()*Property.PAGE_SIZE_MYFEED+position);
			return mCursor;
		}
	
	 
	 public View getView(int position, View convertView, ViewGroup parent) {
	       
		    if(mCursor==null){
		    	 throw new IllegalStateException("this should only be called when the cursor is valid");
		    }
		 
	        if (!mCursor.moveToPosition(getPage()*Property.PAGE_SIZE_MYFEED+position)&&position==(getCount()-1)) {
	        	mType=ITEM_MORE;
	        }
	        View v;
	        if (convertView == null) {
	            v = newView(mContext, mCursor, parent);
	        } else {
	            v = convertView;
	        }
	        bindView(v, mContext, mCursor);
	        return v;
	    }


	public void setPage(int page) {
		this.page = page;
	}


	public int getPage() {
		return page;
	}
	
}
